import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Talk from '../components/Talk.vue'
import Comment from '../components/Comment.vue'
import Document from '../components/Document.vue'
import Luntan from '../components/Luntan.vue'
import Project from '../components/Project.vue'
import Question from '../components/Question.vue'
import Tags from '../components/Tags.vue'

Vue.use(VueRouter)

const routes = [
  { path: '/', component: Login },
  {
    path: '/talk',
    component: Talk,
    children: [
      { path: '/Comment', component: Comment },
      { path: '/Document', component: Document },
      { path: '/Luntan', component: Luntan },
      { path: '/Project', component: Project },
      { path: '/Question', component: Question },
      { path: '/Tags', component: Tags }
    ]
  }
]
const router = new VueRouter({
  routes
})
// 挂在路由导航守卫
router.beforeEach((to, from, next) => {
  // to 将要访问得路径
  // from 代表冲那个路径跳转而来
  // next 是一个函数,表示放行
  if (to.path === '/') return next()
  // 获取token
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/')
  next()
})

export default router
